package finalproject.mobapp.Main;

import java.util.HashMap;
import java.util.List;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

import finalproject.mobapp.R;
import finalproject.mobapp.ARPackage.MapOverlay;
import finalproject.mobapp.ARPackage.Poi;
import finalproject.mobapp.ARPackage.PoiRetrieval;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MapViewActivity extends MapActivity {
	
	Context context = this;
	
	LocationManager locationManager;
	Location gps_loc;
	Location net_loc;
	boolean gps_enabled = false;
	boolean network_enabled = false;
	
	MapView myMapView;
	MapController mapController;
	MyLocationOverlay myLocationOverlay;
	List<Overlay> overlays;
	MapOverlay poiOverlays;

	Bundle extras;
	String category = "";
	int radius = 0;
	Location myLocation;
	HashMap<Integer, Poi> poiLocations;
	
	static final private int MENU_ARVIEW = Menu.FIRST;
	static final private int MENU_LISTVIEW = Menu.FIRST + 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapview);
        
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        
        // get MapView and configure
        myMapView = (MapView) findViewById(R.id.myMapView);      
        myMapView.setSatellite(false);
        myMapView.setStreetView(false);
        
        // get MapController and configure
        mapController = myMapView.getController();
        mapController.setZoom(15);
        
        // add overlay
        overlays = myMapView.getOverlays();
        poiOverlays = new MapOverlay(getApplicationContext());
        overlays.add(poiOverlays);
        
        // add MyLocationOverlay
        myLocationOverlay = new MyLocationOverlay(this, myMapView);
        myLocationOverlay.enableMyLocation();
        overlays.add(myLocationOverlay);
        
        // get extras info
        extras = getIntent().getExtras();
        if (extras != null) {
        	category = extras.getString("category");
        	radius = extras.getInt("radius");
        }
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		
		if (gps_enabled) {
        	gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        	if (gps_loc != null) {
				myLocation = gps_loc;
			}
        }
        
        if (network_enabled) {
        	net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        	if (net_loc != null) {
				if (gps_loc == null) {
					myLocation = net_loc;
				}
			}
        }
        
        if (myLocation != null) {
            updateMapView();
        }
        
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
	}
	
	LocationListener gpsListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			if (location != null) {
				updateMapView();
			}
		}
	};
	
	LocationListener networkListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			if (location != null) {
				updateMapView();
			}
		}
	};
	
	@Override
	public void onStop() {
		locationManager.removeUpdates(gpsListener);
		locationManager.removeUpdates(networkListener);
		myLocationOverlay.disableMyLocation();
		
		super.onStop();
	}
	
	public void updateMapView() {
		// update poiLocations
		poiLocations = PoiRetrieval.GetNearbyPois(category, radius, myLocation);
		
		//update map overlay
		poiOverlays.setPoiLocations(poiLocations);
		myMapView.invalidate();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ARVIEW, Menu.NONE, "AR View");
        menu.add(0, MENU_LISTVIEW, Menu.NONE, "List View");
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		Intent intent = new Intent();
		
		switch (item.getItemId()) {
		case (MENU_ARVIEW):
			// Start the ARViewActivity
        	intent.setClass(context, ARViewActivity.class);
        	intent.putExtra("category", category);
        	intent.putExtra("radius", radius);
    		
    		MapViewActivity.this.finish();
        	startActivity(intent);
        	return true;
		
		case (MENU_LISTVIEW):
			// Start the ARListViewActivity
        	intent.setClass(context, ARListViewActivity.class);
        	intent.putExtra("category", category);
        	intent.putExtra("radius", radius);
    		
    		MapViewActivity.this.finish();
        	startActivity(intent);
        	return true;

		default:
			break;
		}
		
		// Return false if you have not handled the menu item.
		return false;
    }
}
